package com.gopay.bis.web.controller.ebank;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.common.util.constants.BPSOrderConstants;
import com.gopay.bis.dao.bankorder.BankOrderDAO;
import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.util.RSAEncoderUtil;

/**
 * 银行回调接口
 * 
 * @BankPayResCallback.java
 * @author LILILI
 */
@Controller
@Scope("prototype")
public class BankPayResCallbank extends BankResBase {

	private static Log logger = LogFactory.getLog(BankPayResCallbank.class);
	@Resource(name = "bankOrderDAO")
    private BankOrderDAO bankOrderDAO;
	@Override
	@RequestMapping(value = "/bankPayResCallback", method = {RequestMethod.POST, RequestMethod.GET })
	protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
		logger.error("----BankPayResCallback 接收银行支付回调处理开始  ----");
		// 打印请求参数
		this.displayRequestParameters(request, logger);
		// 银行码 + 交易类型
		String bank = request.getParameter("bankCode") + " "+ request.getParameter("orderType");
		logger.error("回调请求处理码retCode：" + request.getParameter("retCode"));
		if (BPSOrderConstants.RETCODE_02.equals(request.getParameter("retCode"))) {
			logger.error(bank + "回调处理，原通知参数解析有误，通知网关");
		}
		String payStatus = request.getParameter("payStatus");
		logger.error("回调请求处理码订单状态：" + payStatus);
		if (!BPSOrderConstants.PAY_STATUS_Y.equals(payStatus) && !BPSOrderConstants.PAY_STATUS_N.equals(payStatus)) {
			logger.error(bank + "回调处理，原订单状态有误，通知网关");
		}
		logger.error(bank + "回调处理，原通知报文解析正确，进行后续处理：orderId="+ request.getParameter("orderId") + "|orderAmt="+ request.getParameter("amt") + "|orderStatus="+ request.getParameter("payStatus"));
		// 银行回调通知
		request.setAttribute(BPSOrderConstants.TXN_CODE_GOPAY,BPSOrderConstants.TXN_CODE_BANK_CALL_BACK);
		String viewName = bisConfig.get("BIS.NOTIFY.CASHIER.FORWARD");
		String notifyUrl = bisConfig.get("BIS.CLIENT.NOTIFY.CASHIER.BANK.RES");
		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setOrderId(request.getParameter("orderId"));
		orderInfo.setAmt(request.getParameter("amt"));
		orderInfo.setNotifyNum("1");
		orderInfo.setPayStatus(request.getParameter("payStatus"));
		orderInfo.setRetCode(request.getParameter("retCode"));
		orderInfo.setRetMsg("");
		orderInfo.setBankName(request.getParameter("bankName"));
		orderInfo.setCardType(request.getParameter("cardType"));
		orderInfo.setSign(super.sign(orderInfo));
		ModelAndView model = new ModelAndView();
		model.setViewName(viewName);
		model.addObject("notifyUrl", notifyUrl);
		model.addObject("orderInfo", orderInfo);
		logger.error(orderInfo);
		logger.error(notifyUrl);
		return model;
	}

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return false;
	}
}
